package client;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


/**
 * 
 * database and server files directory configuration
 *
 */
public class Configuration extends JFrame implements ActionListener
{
	JFrame frame;
	JTextField hostDir;
	JTextField user;
	JTextField pass;
	JTextField filesDir;
	JButton ok;
	JButton def;
	JButton back;
	public setImage img;
	/**
	 * @param owner
	 */
	public Configuration() 
	{
		img = new setImage("conf");
		frame = new JFrame("Zer Lah - Configuration"); 
		frame.setPreferredSize(new Dimension(410, 320));
		frame.setResizable(false);
		
		frame.setLocation((int)Main.dim.getWidth()/2-205, (int)Main.dim.getHeight()/2-160);
		
		hostDir = new JTextField();
		hostDir.setBounds(120, 75, 200, 25);
		hostDir.setText(Main.sets[0]);
		
		user = new JTextField();
		user.setBounds(120, 115, 200, 25);
		user.setText(Main.sets[1]);
		
		pass = new JTextField();
		pass.setBounds(120, 155, 200, 25);
		pass.setText(Main.sets[2]);
		
		filesDir = new JTextField();
		filesDir.setBounds(120, 195, 200, 25);
		filesDir.setText(Main.sets[3]);
		
		ok = new JButton("Save");
		ok.setBounds(275, 240, 100, 25);
		ok.addActionListener(this);
		
		def = new JButton("Set Default");
		def.setBounds(150, 240, 100, 25);
		def.addActionListener(this);
		
		back = new JButton("Close");
		back.setBounds(25, 240, 100, 25);
		back.addActionListener(this);
		
		frame.add(filesDir);
		frame.add(pass);
		frame.add(user);
		frame.add(hostDir);
		frame.add(ok);
		frame.add(def);
		frame.add(back);
		
		frame.setEnabled(true);
		frame.add(img);
		frame.pack();
		frame.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == ok)
		{			
			if(hostDir.getText().equals(""))
				hostDir.setText("jdbc:mysql://localhost/test");
			if(user.getText().equals(""))
				user.setText("root");
			if(pass.getText().equals(""))
				pass.setText("Braude");
			if(filesDir.getText().equals(""))
				filesDir.setText("d:\\Files");
			
			Main.sets[0] = hostDir.getText();
			Main.sets[1] = user.getText();
			Main.sets[2] = pass.getText();
			Main.sets[3] = filesDir.getText();
			
			JOptionPane.showMessageDialog(null,"JDBC Configuration Saved. If no input, default settings will be saved");
		}
		if(e.getSource() == def)
		{
			hostDir.setText("jdbc:mysql://localhost/test");
			user.setText("root");
			pass.setText("Braude");
			filesDir.setText("d:\\Files");
		}
		if(e.getSource() == back)
		{
			Main.log.setEnabled(true);
			Main.log.setVisible(true);
			this.frame.dispose();
		}		
	}
}